<?php

namespace app\controller;


use app\exception\MyBusinessException;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\PlayerScoreRecord;
use support\Db;
use support\Request;
use plugin\admin\app\model\Player;
use plugin\admin\app\model\LoginLog;

class PlayerController
{
    /**
     * 玩家注册
     * @param Request $request
     * @return string
     */
    public function register(Request $request)
    {
        $data = $request->all();

        if(Player::query()->where('username', $data['username'])->exists()) {
            throw new MyBusinessException('用户名重复！' , 300);
        } else {
            try {
                $player = new Player();
                $player->username = $data['username'];
                $player->password = Util::passwordHash($data['password']);
                $player->sex = $data['sex'];
                $player->phone = $data['phone'];
                $player->email = $data['email'];
                $player->save();
                Db::commit();
            } catch (\Throwable $e) {
                Db::rollBack();
                throw new MyBusinessException('参数错误，记录失败！'. $e, 404);
                // return '记录失败：' . $e;
            }
            return '纪录成功！';
        }
    }

    /**
     * 登陆日志日志
     * @param Request $request
     * @return string
     */
    public function login(Request $request)
    {
        $data = $request->all();

        var_dump(md5($data['password'] . '123456'));
        if(Player::query()->where('username', $data['username'])->where('password', md5($data['password'] . '123456'))->exists()) {
            try {
                $login_log = new LoginLog();
                $login_log->player_id = Player::query()->where('username', $data['username'])->value('number');
                $login_log->ip = $request->getRealIp();
                $login_log->save();
                Db::commit();
            } catch (\Throwable $e) {
                Db::rollBack();
                return '记录失败：' . $e;
            }
            return '登陆成功';
        } else {
            return '密码或用户名错误';
        }

    }

    /**
     * 记账
     * @param Request $request
     * @return string
     */
    public function checkscore(Request $request)
    {
        $data = $request->all();
        $username = $request->header('username');

        $player = Player::query()->where('username', $username)->first();

        try {
            $player_score_record = new PlayerScoreRecord();
            $player_score_record->player_id = $player['id'];
            $player_score_record->charge = $data['charge'];
            $player_score_record->score = $player['score']+$data['charge'];
            $player_score_record->save();
            $player->score = $player['score']+$data['charge'];
            $player->save();
            Db::commit();
        } catch (\Throwable $e) {
            Db::rollBack();
            return '记录失败：' . $e;
        }
        return '积分变化记录成功';
    }
    /**
     * 记账
     * @param Request $request
     * @return string
     */
    public function checkscores(Request $request)
    {
        $data = $request->all();
        $username = $request->header('username');

        $player = Player::query()->where('username', $username)->first();

        try {
            Db::table('player_score_record')->insert([
                ['player_id' => $player['id'], 'password' => $data['charge'], 'score' => $player['score']+$data['charge']]
            ]);
            Db::table('player')->where('id', $player['id'])->update(['score', $player['score']+$data['charge']]);
            Db::commit();
        } catch (\Throwable $e) {
            Db::rollBack();
            return '记录失败：' . $e;
        }
        return '积分变化记录成功';
    }

    public function puts(Request $request)
    {
        return $request->all()['username'];
    }
}